package sign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.*;

/**
 * Created by chenxin on 2017/9/12.
 */
public class SignUtil {
    private static final String REQUEST_PARAM_SIGN = "sign";

    public static String caclSign(Map<String, Object> params){
        String signStr = genSignStr(params);
        System.out.println(signStr);
        signStr += "your_sign_key";
        return MD5Util.getMD5ofStr(signStr).toLowerCase();
    }

    public static String genSignStr(Map<String, Object> params){
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);

        StringBuilder signStr = new StringBuilder();
        for (String key : keys){
            Object value = params.get(key);
            if(key.equalsIgnoreCase(REQUEST_PARAM_SIGN) || value == null || value.equals("")){
                continue;
            }
            signStr.append(key);
            signStr.append(value);
        }

        return signStr.toString();
    }

    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("developerId", "100120");
        params.put("epoiId", "123");
        params.put("sign", "[\n" +
                "        {\n" +
                "            \"tableId\":123,\n" +
                "            \"tableName\":\"测试1\",\n" +
                "            \"tableType\":1,\n" +
                "            \"tableLocationDesc\":\"2\",\n" +
                "            \"tableLocationFeature\":0,\n" +
                "            \"room_feature\":0,\n" +
                "            \"serviceFee\":0,\n" +
                "            \"roomFee\":20,\n" +
                "            \"min_people\":3,\n" +
                "            \"max_people\":20        \n" +
                "        },\n" +
                "        {\n" +
                "            \"tableId\":123,\n" +
                "            \"tableName\":\"2\",\n" +
                "            \"tableType\":0,\n" +
                "            \"tableLocationDesc\":\"1\",\n" +
                "            \"tableLocationFeature\":0,\n" +
                "            \"room_feature\":0,\n" +
                "            \"serviceFee\":0,\n" +
                "            \"roomFee\":20,\n" +
                "            \"min_people\":3,\n" +
                "            \"max_people\":20    \n" +
                "        }\n" +
                "    ]");

        String body = "{\n" +
                "    \"appAuthToken\":\"3ea628b3a3478bb6\",\n" +
                "    \"charset\":\"utf-8\",\n" +
                "    \"timestamp\":124,\n" +
                "    \"sign\":\"b6a400fee862ab0646a3a758f5d91c2b\",\n" +
                "    \"epoiId\":456,\n" +
                "    \"tables\":[\n" +
                "        {\n" +
                "            \"tableId\":123,\n" +
                "            \"tableName\":\"测试1\",\n" +
                "            \"tableType\":1,\n" +
                "            \"tableLocationDesc\":\"2\",\n" +
                "            \"tableLocationFeature\":0,\n" +
                "            \"room_feature\":0,\n" +
                "            \"serviceFee\":0,\n" +
                "            \"roomFee\":20,\n" +
                "            \"min_people\":3,\n" +
                "            \"max_people\":20        \n" +
                "        },\n" +
                "        {\n" +
                "            \"tableId\":123,\n" +
                "            \"tableName\":\"2\",\n" +
                "            \"tableType\":0,\n" +
                "            \"tableLocationDesc\":\"1\",\n" +
                "            \"tableLocationFeature\":0,\n" +
                "            \"room_feature\":0,\n" +
                "            \"serviceFee\":0,\n" +
                "            \"roomFee\":20,\n" +
                "            \"min_people\":3,\n" +
                "            \"max_people\":20    \n" +
                "        }\n" +
                "    ]\n" +
                "}";
        Map<String, Object> paramMap = JSON.parseObject(body, new TypeReference<Map<String, Object>>(){});
        System.out.println(paramMap);
        System.out.println(paramMap.size());
        System.out.println(caclSign(paramMap));
    }
}
